package com.ssyc.student.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UIHelper;
import com.ssyc.common.view.flowlayout.FlowLayout;
import com.ssyc.common.view.flowlayout.TagAdapter;
import com.ssyc.common.view.flowlayout.TagFlowLayout;
import com.ssyc.common.view.flowlayout.TagView;
import com.ssyc.student.R;
import com.ssyc.student.activity.StudentGrammarDetectionActivity;
import com.ssyc.student.bean.GrammarDetectionInfo.DataBean.QuestionListBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生端--语法检测--连词成句fragment
 * Created by Kevin on 2018/5/30
 */

public class StudentGrammarConjunctionsFragment extends LazyBaseFragment implements View.OnClickListener {


    //提供选项的数据集合
    private List<String> answerInfos;


    //当前题目的位置
    private int currPos;


    //通知Acitvity进行ViewPager翻页的标识
    public static String VPSCOLL = "StudentGrammarDetectionActivityVpScoll";

    //当前题目的正确答案,通过传值获取
    public String correctAnswer = "rightAnswer";

    //流式布局
    public TagFlowLayout mFlowLayout;

    //已选答案的集合
    private List<Integer> choiceList;

    //显示选择内容
    private TextView tv_content;

    //清除按钮
    private TextView tv_clean;

    //确定按钮
    private TextView tv_sure;

    //流式布局适配器
    private TagAdapter adapter;

    //题目内容
    private List<QuestionListBean.ConjunctionBean> datas;

    //答案
    private TextView tv_show_answer;

    //答案栏
    private LinearLayout ll_show_answer;

    //我的作答栏
    private LinearLayout ll_myAmswer;

    private ImageView iv_answerTF;

    /**
     * 提供的生成当前Fragment的方法
     *
     * @param data 包含页面数据的json字符串
     * @return
     */
    public static StudentGrammarConjunctionsFragment newInstance(String data, int pos) {

        StudentGrammarConjunctionsFragment studentGrammarConjunctionsFragment = new StudentGrammarConjunctionsFragment();
        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        bundle.putInt("pos", pos);
        studentGrammarConjunctionsFragment.setArguments(bundle);

        return studentGrammarConjunctionsFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_student_grammar_conjunctions;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
        //初始化视图
        initView(view);

        showContent();

        //答案选项的数据
        initData();

        //设置展示答案的ListView
        initLv();
    }

    /**
     * 判断是否提交
     */
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (getUserVisibleHint()) {
            if (StudentGrammarDetectionActivity.isCommit == 1) {

                for(int i=0;i<mFlowLayout.getChildCount();i++){

                    TagView tagView= (TagView) mFlowLayout.getChildAt(i);
                    tagView.setEnabled(false);
                }

                //隐藏按钮
                tv_clean.setVisibility(View.GONE);
                tv_sure.setVisibility(View.GONE);

                //显示答案
                ll_show_answer.setVisibility(View.VISIBLE);

                //你的作答显示正误
                ll_myAmswer.setVisibility(View.VISIBLE);

                //判断正误
                if(StudentGrammarDetectionActivity.getAnswerMap().get(currPos)){
                    iv_answerTF.setImageDrawable(getActivity().getResources().
                            getDrawable(R.drawable.stu_grammar_right));
                }else{
                    iv_answerTF.setImageDrawable(getActivity().getResources().
                            getDrawable(R.drawable.stu_grammar_wrong));
                }

            }
        }
    }

    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {

        mFlowLayout = view.findViewById(R.id.id_flowlayout);

        tv_content = view.findViewById(R.id.tv_content);

        tv_clean = view.findViewById(R.id.tv_clean);

        tv_sure = view.findViewById(R.id.tv_sure);

        tv_clean.setOnClickListener(this);

        tv_sure.setOnClickListener(this);

        tv_show_answer = view.findViewById(R.id.tv_show_answer);

        ll_show_answer = view.findViewById(R.id.ll_show_answer);

        ll_myAmswer = view.findViewById(R.id.ll_myAmswer);

        iv_answerTF = view.findViewById(R.id.iv_answerTF);
    }


    /**
     * 答案选项
     */
    private void initData() {

        //已选答案集合
        choiceList = new ArrayList<>();
        //获取当前页面的位置
        currPos = getArguments().getInt("pos");

        //获取传递过来的页面数据
        String dataJson = getArguments().getString("data");
        //解析该Json获取页面数据
        datas = new ArrayList<>();
        datas = GsonUtil.jsonToBean(dataJson, QuestionListBean.class).getConjunction();
        answerInfos = new ArrayList<>();
        answerInfos = datas.get(currPos).getDetatil();

        tv_show_answer.setText(datas.get(currPos).getAnswer());


    }

    /**
     * 设置流式布局
     */
    private void initLv() {

        final LayoutInflater mInflater = LayoutInflater.from(getActivity());

        adapter = new TagAdapter<String>(answerInfos) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.stu_grammar_conjunctions_tv,
                        mFlowLayout, false);
                tv.setText(s);
                return tv;
            }

            @Override
            public void onSelected(int position, View view) {

                super.onSelected(position, view);

                ((TextView) view).setTextColor(getResources().getColor(R.color.white));
            }

            @Override
            public void unSelected(int position, View view) {
                super.unSelected(position, view);
                ((TextView) view).setTextColor(getResources().getColor(R.color.black));
            }
        };

        //添加适配器
        mFlowLayout.setAdapter(adapter);

        //流式布局点击事件
        mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                int ishas = -1;
                for (int i = 0; i < choiceList.size(); i++) {
                    if (choiceList.get(i) == position) {
                        ishas = i;
                    }
                }
                //判断是否存在
                if (ishas != -1) {
                    choiceList.remove(ishas);
                } else {
                    choiceList.add(position);
                }

                //初始化字符串
                String content = "";

                for (int i = 0; i < choiceList.size(); i++) {
                    if (i == 0) {
                        content += answerInfos.get(choiceList.get(i));
                    } else {
//                        //句末标点
//                        if (".".equals(answerInfos.get(choiceList.get(i)))) {
//                            content += answerInfos.get(choiceList.get(i));
//                        } else {
//                            content += " " + answerInfos.get(choiceList.get(i));
//                        }
                        content += " " + answerInfos.get(choiceList.get(i));
                    }

                }
                //设置已选内容展示
                tv_content.setText(content);
                return true;
            }
        });
    }

    /**
     * 点击事件
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_clean) {
            //清除按钮
            mFlowLayout.setAdapter(adapter);
            choiceList.clear();
            tv_content.setText("");
        } else if (id == R.id.tv_sure) {
            //确定按钮
            if (choiceList.size() == 0) {
                UIHelper.showToast(getActivity(), "答案不能为空，请选择单词");
                return;
            }

            //判断是否全部选中
            if(choiceList.size()==answerInfos.size()){

            //将选择的答案保存到Acitivity的Map中
            StudentGrammarDetectionActivity.getChooseAnswerMap().put(currPos, tv_content.getText().toString());

            //答案
            correctAnswer = datas.get(currPos).getAnswer();

            //重新做题后需要通知提交界面的适配器数据刷新
            EventBus.getDefault().post(new BusInfo(StudentGrammarCommitFragment.HASDONE, currPos, tv_content.getText().toString()));

            //判断选择答案的对错,然后保存到Map中
            if (tv_content.getText().toString().equals(correctAnswer)) {
                //选择正确
                StudentGrammarDetectionActivity.getAnswerMap().put(currPos, true);
            } else {
                //选择错误
                StudentGrammarDetectionActivity.getAnswerMap().put(currPos, false);
            }
            }

            //选择后跳转到下一页面,EventBus通知Activity进行ViewPager的翻页操作
            EventBus.getDefault().post(new BusInfo(VPSCOLL));
        }
    }
}
